// miniprogram/pages/section/section.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sections: [],
    link: '',
    storage: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    var sections = []
    const link = options.link
    this.setData({
      link: link
    })
    var scrollTop = wx.getStorageSync(link + 'scrollTop') || 0

    var that = this
    wx.request({
      url: 'https://m.bixiadu.com/' + link + 'all.html',
      success: ((res) => {

        var data = res.data
        // console.log(data)
        var list = data.split('<a href="#bottom" style="color: Red;">↓直达页面底部</a>')

        var content = list[1].split('<p class="Readpage" id="bottom"></p>')[0]


        var hrefs = content.split('href="')
        // console.log('href ======= ', hrefs)

        for (const key in hrefs) {
          const href = hrefs[key]
          if (href.indexOf('</a>') != -1) {
            // console.log(href)
            const links = href.split('">')
            // console.log(links)
            const titles = links[1].split('</a>')
            const newData = {
              link: links[0],
              title: titles[0]
            }
            sections.push(newData)
          }

        }
        that.setData({
          sections: sections,
        })
        // console.log(sections)
        // setTimeout(function(){
        wx.pageScrollTo({
          scrollTop: scrollTop,
          duration: 0
        })
        // },500)
        wx.hideLoading()

      })
    })
  },

  tap(e) {
    // console.log('1111')

    const item = e.currentTarget.dataset.item
    // console.log(item)
    wx.navigateTo({
      url: '../richText/richText?firstLink=' + this.data.link + '&secondLink=' + item.link,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var storage = wx.getStorageSync(this.data.link)
    if (storage) {
      this.setData({
        storage: storage
      })

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onPageScroll: function (e) {
    wx.setStorageSync(this.data.link + 'scrollTop', e.scrollTop)
  }
})